package com.markhsiu.result;

import com.google.common.base.Charsets;
import com.markhsiu.tools.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * conflict :29996
 * machine size = 5704
 * 选手所得分数为：5881.811657537431
 * cost : 363 s
 * @author Mark Hsiu
 * @since 2018/7/18 下午8:49
 */
public class AAliRun {

    public static final String ROOT = "src/main/resources/";

    public static final String  PATH_APP = AAliRun.ROOT + "data/app_resources.csv";
    public static final String  PATH_APP_INTER = AAliRun.ROOT + "data/app_interference.csv";

    public static final String  PATH_MACHINE = AAliRun.ROOT + "data/machine_resources.a.csv";
    public static final String  PATH_INSTANCE = AAliRun.ROOT + "data/instance_deploy.a.csv";
    public static final String  PATH_JOB = AAliRun.ROOT + "data/job_info.a.csv";
    public static final String  PATH_RESULT = AAliRun.ROOT + "result.a.csv";

    public static void main(String[] args) throws Exception {
        File file = new File(ROOT+"a/a_app_resources.csv");
        System.out.println(file.getAbsoluteFile());

        List<String> params = new ArrayList<String>();
        params.add(PATH_APP);
        params.add(PATH_MACHINE);
        params.add(PATH_INSTANCE);
        params.add(PATH_APP_INTER);

        InputStream problem;


        int deployInstance = 0;
        // 将赛题拼成评测数据
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            List<String> lines = new ArrayList<String>();
            BufferedReader bs = new BufferedReader(new FileReader(new File(params.get(i))));
            for (String line = bs.readLine(); line != null; line = bs.readLine()) {
                if(i == 2){
                    if(line.indexOf("machine")> 0){
                        deployInstance ++;
                    }
                }
                lines.add(line);
            }
            sb.append("" + lines.size()).append("\n");
            for (String line : lines) {
                sb.append(line).append("\n");
            }
        }
        String alldata = sb.toString();
        problem = new ByteArrayInputStream(alldata.getBytes());

        long startTime = System.currentTimeMillis();
        System.out.println("开始测评。。。");
        System.out.println("deployInstance ： "+deployInstance);
        // 评测
        ABadFit evaluator = new ABadFit();
        evaluator.init(new BufferedReader(new InputStreamReader(problem, Charsets.UTF_8)));
        double costs = evaluator.evaluate();
        List<String> result = evaluator.getResult();

        FileUtils.writeLines(PATH_RESULT,result);
        System.out.println("选手所得分数为：" +costs);
        System.out.println("cost : "+ (System.currentTimeMillis()- startTime)/1000 +" s");
    }
}
